package cn.lzj.product.service.impl;

import cn.lzj.basic.exception.BusinessException;
import cn.lzj.basic.service.IBaiduAiAuditService;
import cn.lzj.basic.service.impl.BaseServiceImpl;
import cn.lzj.basic.util.AjaxResult;
import cn.lzj.product.domain.Product;
import cn.lzj.product.mapper.ProductMapper;
import cn.lzj.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        productMapper.offsale(offsaletime,ids);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        for(Long id:ids){
            Product product = productMapper.loadById(id);
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());
        if(textBoo&&imageBoo){
            product.setOnsaletime(new Date());
            product.setState(1);
            productMapper.update(product);
        }else {
            throw new BusinessException("上架失败，可能有非法内容！！");
        }
        }
        return AjaxResult.util();
    }
}
